/**
 * Created by hjqi on 2016/2/26.
 */

module.controller("BuildRepoLstController", function ($scope,$rootScope, $cookies, $stateParams, $uibModal, projectListService, utilService, buildAddition, $mdDialog) {

    // console.log($stateParams.type);
    if ($stateParams.type !== 'undefined') {
      $scope.selectedClient = $stateParams.type;
    }
    $scope.repoType = $scope.selectedClient;

    $scope._cacheMap = {};
    $scope.clientlist = [
      {
        'is_linked': false,
        'is_show': true,
        'name': 'github',
        'src': 'github_logo.png',
        'alias_name': 'Github'
      },
      {
        'is_linked': false,
        'is_show': true,
        'name': 'bitbucket',
        'src': 'bitbucket_logo.png',
        'alias_name': 'Bitbucket'
      }
    ];

    $scope.linked = [{"repo_login": "hjqi", "is_linked": true, "username": "kidqhj"}];

    if ($scope.selectedClient != null) {
      _projListSvc($scope.selectedClient);
    }

    $scope.selectCodeRepo = function () {
      if ($scope.repoType === undefined) {
        $scope.repoType = "github"; //default
      }
      var repoType = $scope.repoType;
      _projListSvc(repoType);
      $scope.selectedClient = repoType;
    };

    $scope.selectClientRepo = function (repoType, name) {

      var scope = $scope.$new(true);
      $scope._cacheMap[repoType].forEach(function (item, index, array) {
        if (item.name == name) {
            scope.item = item;
            $mdDialog.hide(scope);
        }
      });
      // _openModel(scope);

    };

    $scope.cancel = function () {
      $mdDialog.cancel();
    };

    // function _openModel(scope){
    //     var uibModalInstance = $uibModal.open({
    //         animation:true,
    //         templateUrl: 'template/cp/buildList.html',
    //         controller: 'BuildInfosController',
    //         size: undefined,
    //         scope: scope,
    //         resolve: {
    //             items: function () {
    //                 return $scope.items;
    //             }
    //         }
    //     });
    //
    //     uibModalInstance.result.then(function (selectedItem) {
    //         $scope.selected = selectedItem;
    //     }, function () {
    //         console.log('Modal dismissed at: ' + new Date());
    //     });
    // }

    function _projListSvc(repoType) {
      if ($scope._cacheMap.hasOwnProperty(repoType)) {
        $scope.codeRepos = $scope._cacheMap[repoType];
      } else {
        $scope.codeRepos = [];
        var user = $cookies.get("username");
        $scope.repo_namespaces_loading = true;
        projectListService(user, repoType).then(function (response) {
          $scope.repo_namespaces_loading = false;
          if (response.data instanceof Array) {
            $scope.codeRepos = response.data;
            $scope._cacheMap[repoType] = response.data;
          } else
            window.location.href = response.url;
        }, function (response) {
          // alert fail
          alertify.error("get project list failed:" + response.data);
          //console.log(response.data + "gitHubProjectListService failed");
        });
      }

    }

  }
);
